require(['./config'], () => {
    require(['template', 'header', 'footer'], (template) => {
        class Cart {
            constructor() {
                this.init();
                this.cartTotalMoney();
                this.allCheck();
                this.checksChange();
                this.alreadyChoose();
                this.addNum();
                this.redNum();
                this.delItem();
               
            }
            init() {
                let cart = localStorage.getItem('cart');
                if (cart) {
                    //购物车不为空
                   
                    this.cart = JSON.parse(cart);
                    let html = template('cart-show', { cart: this.cart });
                    $('.my-cart').html(html);
                    $('.cart-shop').show();
                    $('.cart-total').show();
                    $('.tt').show();
                    $('.cart-empty').hide();
                } else {
                    //购物车为空
                    let html = template('cart-show', { cart: this.cart });
                    $('.my-cart').html(html);
                    $('.tt').hide();
                    $('.cart-shop').hide();
                    $('.cart-total').hide();
                    $('.cart-group').hide();
                    $('.cart-empty').show();

                }
            }


            cartTotalMoney() {
                // console.log(111)
                let cart = localStorage.getItem('cart');
                // console.log(this.cart)
                //会被重复取用
                if (cart) {
                    this.cart = JSON.parse(cart);
                    let allMoney = this.cart.reduce((money, shop) => {
                        if (shop.check) money += shop.num * shop.price;
                        return money;
                    }, 0).toFixed(2)
                    $('#allMoney').html(allMoney);
                    $('#allPrice').html(allMoney);
                } else {
                    $('#allMoney').html(0);
                }
            }



            checksChange() {
                const _this = this;
                // if ($('#allCheck').prop('checked', true)) { $('.check').prop('checked', true) };
                // let cart = localStorage.getItem('cart');
                // localStorage.setItem('cart', JSON.stringify(_this.cart))
                $('.check').on('change', function() {
                    let checkedNum = _this.cart[0].checkedNum;
                    if ($(this).prop('checked')) {
                        checkedNum++;
                        _this.cart = _this.cart.map(shop => {
                            shop.checkedNum = checkedNum;
                            return shop;
                        })
                    } else {
                        checkedNum--;
                        _this.cart = _this.cart.map(shop => {
                            shop.checkedNum = checkedNum;
                            return shop;
                        })
                    };
                    // console.log(checkedNum)
                    if (checkedNum === $('.check').length) {
                        $('#allCheck').prop('checked', true);
                        // _this.cart = _this.cart.map(shop => {
                        //     shop.allCheck = true;
                        //     return shop;
                        // })
                    } else {
                        $('#allCheck').prop('checked', false)
                            // _this.cart = _this.cart.map(shop => {
                            //     shop.allCheck = false;
                            //     return shop;
                            // })
                    }
                    const id = Number($(this).parents('.cart-item').attr('data-id'));
                    // console.log(id)
                    //根据id找数据，修改check属性
                    _this.cart = _this.cart.map(shop => {
                        if (shop.id === id) shop.check = $(this).prop('checked');
                        return shop;
                    })
                    localStorage.setItem('cart', JSON.stringify(_this.cart)); //将改变了的数据再存进localStorage
                    _this.cartTotalMoney();
                    _this.alreadyChoose();
                    //记录选中个数
                    // console.log($(this))
                })
            }
            allCheck() {
                    const _this = this;
                    let cart = localStorage.getItem('cart');
                    let checkedNum = $('.check').length;
                    if (cart) {
                        if (checkedNum === _this.cart[0].checkedNum) { $('#allCheck').prop('checked', true); }
                        // else {
                        //     $('.check').prop('checked', false);
                        // }
                    }
                    _this.cartTotalMoney();
                    $('#allCheck').on('click', function() {
                        if ($(this).prop('checked')) {
                            $('.check').prop('checked', true)
                            _this.cart = _this.cart.map(shop => {
                                shop.check = true;
                                shop.checkedNum = checkedNum;
                                // shop.allCheck = true;
                                return shop;
                            })
                            localStorage.setItem('cart', JSON.stringify(_this.cart)); //将改变了的数据再存进localStorage
                            _this.cartTotalMoney();
                            _this.alreadyChoose();
                        } else {
                            $('.check').prop('checked', false)
                            _this.cart = _this.cart.map(shop => {
                                shop.check = false;
                                shop.checkedNum = 0;
                                // shop.allCheck = false;
                                return shop;
                            })
                            localStorage.setItem('cart', JSON.stringify(_this.cart)); //将改变了的数据再存进localStorage
                            _this.cartTotalMoney();
                            _this.alreadyChoose();
                        }
                    })
                }
                //已选
            alreadyChoose() {
                    let cart = localStorage.getItem('cart');
                    if (cart) {
                        let detail = JSON.parse(cart);
                        // console.log(detail)
                        let allChooseNum = this.cart.reduce((choseNum, shop) => {
                            if (shop.check) choseNum += shop.num;
                            return choseNum;
                        }, 0)
                        $('#allChoose').html(allChooseNum);
                    }

                    // console.log(allChooseNum)
                }
                //加
            addNum() {
                    const _this = this;
                    let num1 = Number($(this).siblings('input').val());
                    let num2 = Number($(this).parent().parent().prev().children('.nowprice').children('.pricenow').html());
                    $('.j-add').on('click', function() {
                        let num1 = Number($(this).siblings('input').val());
                        let num2 = Number($(this).parent().parent().prev().children('.nowprice').children('.pricenow').html());

                        let cart = localStorage.getItem('cart');
                        cart = JSON.parse(cart);
                        const id = Number($(this).parents('.cart-item').attr('data-id'));
                        _this.cart.map((shop) => {
                                if (shop.id === id)
                                    shop.num++;
                                return shop;
                            })
                            // console.log()
                        $(this).siblings('input').val(function() {
                            return this.value - 1 + 2;
                        });
                        localStorage.setItem('cart', JSON.stringify(_this.cart));
                        $(this).parent().parent().next().children().children().last().html((num1 + 1) * num2);
                        _this.cartTotalMoney();
                        _this.alreadyChoose();
                    })
                }
                //减
            redNum() {
                    const _this = this;
                    $('.j-reduce').on('click', function() {

                        let num1 = Number($(this).siblings('input').val());
                        let num2 = Number($(this).parent().parent().prev().children('.nowprice').children('.pricenow').html());
                        if (num1 < 2) {
                            alert('最小数量为1')
                        } else {
                            let cart = localStorage.getItem('cart');
                            cart = JSON.parse(cart);
                            const id = Number($(this).parents('.cart-item').attr('data-id'));
                            _this.cart.map((shop) => {
                                    if (shop.id === id)
                                        shop.num--;
                                    return shop;
                                }, 0)
                                // console.log()
                            $(this).siblings('input').val(function() {
                                return this.value - 1;
                            });
                            localStorage.setItem('cart', JSON.stringify(_this.cart));
                            $(this).parent().parent().next().children().children().last().html((num1 - 1) * num2);
                            _this.cartTotalMoney();
                            _this.alreadyChoose();

                        }
                    })
                }
                //删除
            delItem() {
                    const _this = this;
                    //批量删除
                    $('.mgl30').on('click', function() {
                        let length = $(this).parents('.cart-total').siblings('.my-cartBody').find('.cart-item').length;
                        // console.log(length)
                        if (confirm('确定删除吗？')) {
                            let cart = localStorage.getItem('cart');
                            $('.check').each(function() {
                                if ($(this).prop('checked') === true) {
                                    $(this).parents('.cart-item').remove();
                                }
                            })
                            if (cart) {
                                cart = JSON.parse(cart);
                                cart = cart.filter(shop => {
                                    if (shop.check === false) { shop.checkedNum = length - shop.checkedNum; return shop; }
                                })
                                cart.map(shop => {
                                    shop.check = true
                                    shop.allCheck = true
                                    return shop
                                })
                                localStorage.setItem('cart', JSON.stringify(cart));
                                _this.cartTotalMoney();
                                _this.alreadyChoose();
                                // _this.allChecked();
                                if ($('#allCheck').prop('checked', true)) { $('.check').prop('checked', true) };
                            }
                            if (cart.length === 0) {
                                $('.cart-group').hide();
                                localStorage.removeItem('cart')
                                $('.tt').hide();
                                $('.cart-shop').hide();
                                $('.cart-total').hide();
                                $('.cart-empty').show();
                            }

                        }
                    })
                    $('.del').on('click', function() {
                        if (confirm('确定删除吗？')) {
                            let cart = localStorage.getItem('cart');
                            if (cart) {
                                cart = JSON.parse(cart)
                                const id = Number($(this).parents('.cart-item').attr('data-id'));
                                cart = cart.filter(shop => {
                                    if (shop.id != id) return shop
                                })
                                localStorage.setItem('cart', JSON.stringify(cart));
                                $(this).parents('.cart-item').remove();
                                _this.cartTotalMoney();
                                _this.alreadyChoose();
                                // _this.allChecked();
                            }
                            console.log(cart.length)
                            if (cart.length == 0) {
                                localStorage.removeItem('cart')
                                $('.tt').hide();
                                $('.cart-shop').hide();
                                $('.cart-total').hide();
                                $('.cart-empty').show();
                            }
                        }

                    })
                }
            
        }
        new Cart();
    })
})